const express = require("express");
const cors = require("cors");

const corsOptions = {
  origin: "http://localhost:3000",
  methods: ["GET", "HEAD", "OPTION", "POST", "PUT", "DELETE", "PATCH"],
};

const localCorsOptions = {
  origin: "http://10.138.200.90:3000",
  methods: ["GET", "HEAD", "OPTION", "POST", "PUT", "DELETE", "PATCH"],
};

const bodyParser = require("body-parser");
const requireAuth = require("./middleware/requireAuth");
// const morgan = require("morgan");
const app = express();
//1.MIDLEWARE
app.use(express.json());
app.use(cors(corsOptions));
app.use(bodyParser.json());

//2.ROUTE
const userRoutes = require("./routes/userRoutes");
const recognizeRoutes = require("./routes/recognizeRoutes");
const materialRoutes = require("./routes/materialRoutes");
const companymaterialRoutes = require("./routes/companyMaterialRoutes");
const companyRoutes = require("./routes/companyRoutes");
const goodsRoutes = require("./routes/goodsRoutes");
const { getCredential } = require("./util/aws");

app.use("/user", userRoutes);
app.use("/table", recognizeRoutes);
app.use("/material", materialRoutes);
app.use("/companymaterial", companymaterialRoutes);
app.use("/company", companyRoutes);
app.use("/goods", goodsRoutes);
app.get("/aws", getCredential);

app.get("/", requireAuth, (req, res) => {
  res.send(`your email: ${req.user.email}`);
});

module.exports = app;
